#!/bin/bash
#
# -- ipv4 --
# replace dotquad.c to convert dotted-quad IP address to decimal
#

# https://www.linuxjournal.com/content/validating-ip-address-bash-script
valid_ip() {
	local ip=$1
	local stat=1
	if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
		OIFS=$IFS
		IFS='.'
		ip=($ip)
		IFS=$OIFS
		[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
			&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
		stat=$?
	fi
	return $stat
}

if ! valid_ip $@ ; then
	exit 1
fi

#====================================================================

a=
b=
c=
d=
ip=$@
IFS=. read -r a b c d <<< "$ip"
printf '%d\n' "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))"

exit 0

### END ###